<template>
	<view class="shopCartPage">
		<u-navbar title=" " bgColor="#fff">
			<view slot="left">购物车({{goodsProducts?goodsProducts.length:0}})</view>
		</u-navbar>
		<view class="topBox flex_heng">
		</view>
		<view class="shopCartBox">
			<commonCar :list="goodsProducts" @delect="delect" @accounts="accounts" @addNum="addNum" @subNum="subNum"
				v-if="goodsProducts.length>0">
			</commonCar>
		</view>
	</view>
</template>

<script>
	import {
		getCartList,
		getProductById,
		createOrder,
		putCart,
		getAddressList,
		delCart
	} from '@/api/api.js'

	import commonCar from '@/components/shopCar/shopCar.vue'
	export default {
		data() {
			return {
				imgUrl: this.$imgUrl,
				cartList: [],
				actionOp: [{
					text: '删除'
				}],
				goodsChecked: [],
				checkedAll: [],
				manageType: false,
				goodsProducts: [],
			};
		},
		components: {
			commonCar
		},

		onLoad(op) {},
		onShow() {
			this.getList()
		},

		methods: {
			accounts(e) {
				console.log(e);
				let orderInfo = e.map(item => {
					return {
						shopName: item.shopName,
						totalPrice: item.shoppingCartList.reduce((sum, good) => sum + good.num * good.price, 0),
						goodsInfo: item.shoppingCartList.map(good => ({
							skuId: good.skuId,
							goodsName: good.title,
							img: good.cartData.sku.pic,
							sku: `${JSON.parse(good.cartData.product.data1).skuOneName}:${good.cartData.sku.skuOneValue}/${JSON.parse(good.cartData.product.data1).skuTwoName}:${good.cartData.sku.skuTwoValue}`,
							price: good.price,
							num: good.num,
							shoppingCartId: good.id
						}))
					};
				});

				console.log('orderInfo', orderInfo);
				uni.setStorageSync('orderInfo', orderInfo);
				uni.navigateTo({
					url: '/package/pages/orderDetail/orderDetail'
				})

			},
			addNum(e) {
				this.upDataCartNum(e)
			},
			subNum(e) {
				this.upDataCartNum(e)
			},
			//修改购物车数量
			upDataCartNum(e) {
				const params = {
					num: e.num,
					id: e.id,
				}
				putCart({
					params
				}).then(res => {
					console.log('修改购物车数量', res)
				})
			},

			// 购物车删除
			delect(e) {
				const ids = e
					.flatMap(shop => shop.shoppingCartList.filter(item => item.checked === 2))
					.map(item => item.id)
					.join(',');

				console.log(ids);
				delCart(ids).then(res => {
					if (res.code == 200) {
						this.getList()
					}
				}).catch(err => {
					console.log(err);
				})
			},


			//获取购物车列表
			getList() {
				getCartList().then(res => {
					if (res.code == 200) {
						this.goodsProducts = res.data;
						this.goodsProducts.map(item => {
							item.shoppingCartList.map(items => {
								items.cartData = JSON.parse(items.cartData);
								items.checked == 1;
							})
						})
						console.log('res', this.goodsProducts)
					}
				})
			},


			//全选
			checkAll() {
				console.log(this.checkedAll);
				if (this.checkedAll.length > 0) {
					this.goodsChecked = []
				} else {
					this.goodsChecked = this.cartList.map(e => {
						return e.id
					})
				}
			},

			//管理
			manage() {
				this.manageType = !this.manageType
			},

			//删除
			deleteAll() {
				console.log(this.goodsChecked);
				if (this.goodsChecked.length == 0) {
					uni.$u.toast('请选择需要删除的商品')
					return
				}

				this.$myRequest
					.httpRequest({
						url: `/system/app/shopping/cart/deleteShoppingCart`,
						method: 'POST'
					}, this.goodsChecked).then((res) => {
						if (res.code == 200) {
							console.log('res', res)
							uni.$u.toast('操作成功')
							this.getShopCartList()
						}
					});
			},

			// 购物车数量变化
			numChange(val, item) {
				let subData = {
					id: item.id,
					num: val.value
				}
				this.$myRequest
					.httpRequest({
						url: `/system/app/shopping/cart/updateShoppingCartNum`,
						method: 'GET'
					}, subData).then((res) => {
						if (res.code == 200) {
							console.log('res', res)
						}
					});

			},



			toSettle() {
				console.log(111, this.goodsChecked);
				if (this.goodsChecked.length == 0) {
					uni.$u.toast('未选择商品！')
					return
				}
				let shopCartOrder = []

				this.cartList.map(item => {
					this.goodsChecked.map(items => {
						if (items == item.id) {
							item.cartData.sku.num = item.num
							item.cartData.sku.productName = item.productName
							item.cartData.sku.shoppingCartId = item.id
							shopCartOrder.push(item.cartData.sku)
						}
					})
				})
				console.log('shopCartOrder', shopCartOrder)
				uni.setStorageSync('shopCartOrder', shopCartOrder)

				uni.navigateTo({
					url: '/pages/subPack/payShopCartOrder/payShopCartOrder'
				});
			}

		},
		onHide() {
			this.checkedAll = []
			this.goodsChecked = []
		},
	}
</script>

<style lang="scss">
	page {
		background: #f6f6f6;
		height: 100vh;
		padding-bottom: 150rpx;
	}

	.shopCartPage {
		.topBox {
			margin-top: 174rpx;
			width: 100%;
			background: #fff;
			font-family: HarmonyOS Sans SC, HarmonyOS Sans SC;
			font-weight: 300;
			font-size: 24rpx;
			color: #333333;
			padding: 0 32rpx;
			box-sizing: border-box;

			.topLeft {}

			.topRight {
				.sigonggeImg {
					width: 24rpx;
					height: 24rpx;
				}
			}
		}

		.shopCartBox {
			margin-top: 20rpx;
			// padding: 0 32rpx;
			background: #fff;
			box-sizing: border-box;

			.u-empty {
				height: 73vh;
			}

			.have-goods {
				height: calc(100% - 108rpx - 60rpx);
				box-sizing: border-box;
				overflow: auto;
				border-radius: 20rpx;


				::v-deep .swipe-action {
					border: none !important;

				}

				::v-deep .uni-swipe_box {
					background-color: #FFFFFF;
					margin-bottom: 28rpx;
					border: none !important;
					border-radius: 16rpx;
				}

				.o-g {

					padding-left: 16rpx;
					box-sizing: border-box;

					.goods {
						display: flex;
						align-items: center;
					}

					.goods-d {
						width: 90%;
						display: flex;
						margin-top: 16rpx;
						margin-bottom: 20rpx;

						image {
							width: 160rpx;
							height: 160rpx;
							border-radius: 4rpx;
							margin-right: 20rpx;
							margin-left: 20rpx;
						}

						figcaption {
							width: calc(100% - 160rpx);
							display: flex;
							flex-direction: column;
							justify-content: space-between;
							height: 140rpx;

							.title {
								font-weight: 400;
								font-size: 28rpx;
								color: #555555;
								font-family: 'xiaomi-label';
							}

							.info_text {
								font-weight: 300;
								font-size: 24rpx;
								color: #999999;
								margin: 8rpx 0 32rpx;
							}

							.s-c {
								font-weight: bold;
								font-size: 26rpx;
								color: #E04E3F;
								display: flex;
								justify-content: space-between;
								align-items: center;

								.u-number-box__input {
									background-color: transparent !important;
									font-weight: 400;
									font-size: 24rpx;
									color: #333333;
								}

								.u-number-box__minus {
									width: 22px !important;
								}

								.u-number-box__plus {
									width: 22px !important;
								}

								span {
									font-size: 32rpx;
								}

							}
						}

					}
				}
			}
		}


		.footer {
			background-color: #FFFFFF;
			height: 108rpx;
			position: fixed;
			bottom: 0;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 32rpx;
			box-sizing: border-box;

			image {
				width: 62rpx;
				height: 58rpx;
			}

			.right-btn {
				display: flex;
				align-items: center;

				.total-price {
					display: flex;
					align-items: center;
					font-weight: 400;
					font-size: 28rpx;
					color: #333333;

					b {
						font-weight: 400;
						font-size: 20rpx;
						color: #E04E3F;
					}

					.text_ {
						font-weight: 400;
						font-size: 40rpx;
						color: #E04E3F;
					}
				}
			}

			.u-button {
				width: 220rpx;
				height: 72rpx;
				background: #1A66FF;
				border-radius: 4rpx 4rpx 4rpx 4rpx !important;
				color: #FFFFFF;
				margin-left: 16rpx;
			}
		}

	}


	.flex_heng {
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		justify-content: space-between;
		align-items: center;
	}

	.flex_shu {
		display: flex;
		flex-direction: column;
		flex-wrap: nowrap;
		justify-content: space-between;
		align-items: center;
	}
</style>